<?php
class C_users extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_users');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			case 'listdiv' :
				$this->_listdiv();
				break;
			default :
				$data["s_usergroups"] = $this->m_users->s_groups();
				$data["m_groupdiv"] = $this->m_users->m_groupdiv();
				$data["m_cabang"] = $this->m_public_function->m_cabang();
				$data["t_project"] = $this->m_users->t_project();
				$this->load->view('v_users',$data);
		}
	}
	
    function _insertuserproject($s_users_id, $t_project_id ){
        if ($t_project_id != 0){
            $data = array(
                's_users_id' => $s_users_id, 
                't_project_id' => $t_project_id
            );
            $this->m_users->_insertuserproject($data);
        }
    }
      
    function _clearuserproject($s_users_id){
        $data = array(
			's_users_id' => $s_users_id
        );
		$this->m_users->_clearuserproject($data);
    }
    
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['user_name'] = nosql_post("user_name");
		$data['groups_name'] = nosql_post("groups_name");
		echo $this->m_users->_list($data);
	}
	
	function _insert(){
        
		$divisi = $this->input->post('divisi');
		$data = array(
			'user_name' => $this->input->post('user_name'), 
			's_groups_id' => $this->input->post('user_group'),
			'm_cabang_id' => $this->input->post('m_cabang_id'),  
			'm_groupdiv_id' => $this->input->post('m_groupdiv_id'), 
			'user_passwd' => md5($this->input->post('user_passwd')), 
			  
			'user_active'=> '1');
		$s_users_id = $this->m_users->_insert($data, $divisi);
        
        $t_project_id = $this->input->post('t_project_id');
        $this->_clearuserproject($s_users_id);
        if ( is_array($t_project_id) ){
			for($i=0;$i<count($t_project_id);$i++){
                $this->_insertuserproject($s_users_id,$t_project_id[$i]);
            }
        }else{
                $this->_insertuserproject($s_users_id,$t_project_id);
        }
		echo 1;
	}
	
	function _update(){
		$user_id = $this->input->post('user_id');
		$divisi = $this->input->post('divisi');
		$data = array(
			'user_name' => $this->input->post('user_name'), 
			's_groups_id' => $this->input->post('user_group'),
			'm_cabang_id' => $this->input->post('m_cabang_id'),  
			'm_groupdiv_id' => $this->input->post('m_groupdiv_id'), 
			);
		
		$user_passwd = $this->input->post('user_passwd'); 
		
		if ($user_passwd != "")
			$data["user_passwd"] = md5($user_passwd);
			
		$this->m_users->_update($data,$user_id,$divisi);
        $s_users_id = $user_id;
        
        $t_project_id = $this->input->post('t_project_id');
        $this->_clearuserproject($s_users_id);
        if ( is_array($t_project_id) ){
			for($i=0;$i<count($t_project_id);$i++){
                $this->_insertuserproject($s_users_id,$t_project_id[$i]);
            }
        }else{
                $this->_insertuserproject($s_users_id,$t_project_id);
        }
		echo 1;
 	}
	
	function _delete(){
		$user_id= $this->input->post('user_id');
		$data = array(
			'user_active' => '0'
		);
		$this->m_users->_delete($data,$user_id);
		echo 1;
	}
	
	function _listdiv(){
		echo json_encode($this->m_users->_listdiv());
	}
}